找传奇、传世资源到传世资源站!

C# GDI+绘制圆形进度条制作自定义控件

8.5玩家评分(1人评分)
下载后可评
介绍 评论 失效链接反馈

C# GDI 绘制圆形进度条制作自定义控件from clipboard using System;using System.Windows.Forms;using System.ComponentModel;using System.Drawing;using System.Drawing.Drawing2D;namespace ProgressCircleControl{ /// <summary> /// 圆形进度条 /// </summary> public class ProgressCircle : Control { #region variables Timer timer; Graphics canvasGraphic; Image canvas; Image icon; Rectangle rect; int blinkCount = 2; int blinkSpeed = 10; Pen spokePen = Pens.White; int spokeCount = 4; Brush pieBrush = Brushes.LawnGreen; int value = 0; PointF center; #endregion #region properties [DefaultValue(2), Category("Appearance"), Description("Blink count. 0 for disable.")] public int BlinkCount { get { return blinkCount; } set { if (value > -1) { blinkCount = value; Refresh(); } } } [DefaultValue(10), Category("Appearance"), Description("Blink speed.")] public int BlinkSpeed { get { return blinkSpeed; } set { if (value > 0 && value < 256) blinkSpeed = value; } } [DefaultValue(4), Category("Appearance"), Description("Spoke count. Maximum value is 10. 0 for disable.")] public int SpokeCount { get { return spokeCount; } set { spokeCount = value; Invalidate(); } } [DefaultValue(typeof(Color), "White"), Category("Appearance"), Description("Color of spokes.")] public Color SpokeColor { get { return spokePen.Color; } set { spokePen.Color = value; Invalidate(); } } [Category("Appearance"), Description("Icon to display. Maximum size is half of the control.")] public Image Image { get { return icon; } set { icon = value; Invalidate(); } } [DefaultValue(0), Category("Behavior"), Description("Value of percentage.")] public int Value { get { return value; } set { if (value < 0) value = 0; if (value > 100) value = 100; this.value = value; Invalidate(); if (value == 100 && BlinkCount != 0) timer.Start(); else if (timer.Enabled) timer.Stop(); } } private Color valueTextColor = Color.OrangeRed; [Category("Behavior"), Description("进度值的文字颜色,默认是橙红色.")] public Color ValueTextColor { get { return valueTextColor; } set { valueTextColor = value; } } #endregion #region initiators public ProgressCircle() { DoubleBuffered = true; Width = 64; ForeColor = Color.FromArgb(146, 223, 99); spokePen = new Pen(BackColor, 1); pieBrush = new SolidBrush(ForeColor); BackColor = Color.White; ForeColor = Color.LawnGreen; BlinkCount = 2; SpokeColor = Color.White; SpokeCount = 4; center = new PointF(); timer = new Timer(); timer.Interval = 10; timer.Tick = new EventHandler(timer_Tick); OnResize(null); } #endregion #region callbacks int blink = 0; int count = 255; void timer_Tick(object sender, EventArgs e) { if (!timer.Enabled) return; if (blink == BlinkCount) { blink = 0; count = 255; timer.Stop(); Refresh(); } else { count -= 5; if (count == 0) { count = 255; blink ; } else { Refresh(); } } } #endregion #region painters void drawIcon(Graphics g, Image img) { if (img == null) return; g.DrawImageUnscaled(img, (this.ClientSize.Width - img.Width) / 2, (this.ClientSize.Height - img.Height) / 2); } void drawSpokes(Graphics g, int count) { if (count < 1) return; if (count > 10) count = 10; float angleStep = 360f / (count * 2); float angle = 0f; for (int i = 0; i < count; i ) { PointF p1 = angleToPoint(angle, rect.Width / 2); PointF p2 = angleToPoint(angle 180f, rect.Width / 2); g.DrawLine(spokePen, p1, p2); angle = angleStep; } } void drawProgress(Graphics g, float percent) { if (percent < 0 || percent > 100) return; g.FillPie( pieBrush, rect, -90, (float)(360f * Value / 100f) ); } void drawBackground(Graphics g) { g.Clear(Color.Transparent); ((SolidBrush)pieBrush).Color = BackColor; g.FillEllipse(pieBrush, rect); ((SolidBrush)pieBrush).Color = this.ForeColor; } #endregion #region helpers PointF angleToPoint(float angle, float radius) { return new PointF( center.X radius * (float)Math.Cos(angle * Math.PI / 180), center.Y radius * (float)Math.Sin(angle * Math.PI / 180) ); } #endregion #region events protected override void OnBackColorChanged(EventArgs e) { base.OnBackColorChanged(e); } protected override void OnForeColorChanged(EventArgs e) { base.OnForeColorChanged(e); ((SolidBrush)pieBrush).Color = ForeColor; } protected override void OnResize(EventArgs e) { this.Height = this.Width; rect = ClientRectangle; rect.Width -= 1; rect.Height -= 1; center = new PointF(rect.Width / 2, rect.Height / 2); if (!this.ClientSize.IsEmpty) { if (canvas != null) canvas.Dispose(); canvas = new Bitmap(rect.Width, rect.Height); if (canvasGraphic != null) canvasGraphic.Dispose(); canvasGraphic = Graphics.FromImage(canvas); } } protected override void OnPaint(PaintEventArgs e) { base.OnPaint(e); e.Graphics.Clear(Parent.BackColor); canvasGraphic.SmoothingMode = SmoothingMode.HighQuality; drawBackground(canvasGraphic); if (!timer.Enabled || blink == BlinkCount) drawProgress(canvasGraphic, value); else { ((SolidBrush)pieBrush).Color = Color.FromArgb(count, ForeColor.R, ForeColor.G, ForeColor.B); canvasGraphic.FillEllipse(pieBrush, rect); ((SolidBrush)pieBrush).Color = ForeColor; } drawSpokes(canvasGraphic, SpokeCount); if (icon != null) drawIcon(canvasGraphic, icon); e.Graphics.DrawImageUnscaled(canvas, (ClientSize.Width - canvas.Width) / 2, (ClientSize.Height - canvas.Height) / 2); if (value != 0) { var rect = ClientRectangle; rect.Inflate(0 - Width / 2, 0 - Height / 2); StringFormat sf = new StringFormat(); sf.LineAlignment = StringAlignment.Center; sf.Alignment = StringAlignment.Center; e.Graphics.DrawString(value.ToString() "%", new Font("微软雅黑", 18F, FontStyle.Bold), new SolidBrush(ValueTextColor), rect, sf); } } #endregion }}

评论

发表评论必须先登陆, 您可以 登陆 或者 注册新账号 !


在线咨询: 问题反馈
客服QQ:174666394

有问题请留言,看到后及时答复